unit PesquisaLancamento;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, UFormPesquisa, DB, DBTables, StdCtrls, Grids, DBGrids, Buttons,
  ExtCtrls, DBCtrls, Mask, ToolEdit, CheckLst;

type
  TFPesquisaLancamento = class(TFormPesquisa)
    Label11: TLabel;
    ELancamento: TEdit;
    Label1: TLabel;
    Label3: TLabel;
    DBLCBConta: TDBLookupComboBox;
    Label8: TLabel;
    DBLCBPlano: TDBLookupComboBox;
    Label7: TLabel;
    DEDataIni: TDateEdit;
    Label2: TLabel;
    DEDataFim: TDateEdit;
    DSConta: TDataSource;
    DSPlano: TDataSource;
    CkBTodasEmpresas: TCheckBox;
    Label10: TLabel;
    CLBEmpresas: TCheckListBox;
    Label4: TLabel;
    ECheque: TEdit;
    EDocumento: TEdit;
    procedure BtnConsultarClick(Sender: TObject);
    procedure CkBTodasEmpresasClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure BtnSelecionarClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure BtnLimparClick(Sender: TObject);
    procedure DBLCBPlanoKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FPesquisaLancamento: TFPesquisaLancamento;
  s : String;
implementation

uses UGeral, Lancamento;

{$R *.dfm}

procedure TFPesquisaLancamento.BtnConsultarClick(Sender: TObject);
begin
 if (EmpresasDesmarcadas(CLBEmpresas) = CLBEmpresas.Items.Count) then
  CkBTodasEmpresasClick(Sender);
 S:=Concatenaempresas(CLBEmpresas);

 QPesquisa.Close;
 QPesquisa.SQL.Clear;
 QPesquisa.SQL.Add(' Select Lanc_Codigo ,Lanc_Data,Lanc_Historico1,lanc_Valor from Lancamento ');
 QPesquisa.SQL.Add(' Where (Codigoempresa in ( ' + s + ') )');

 if ELancamento.Text <> '' then
  begin
   QPesquisa.SQL.Add(' And (Lanc_Codigo = :Lanc)');
   QPesquisa.ParamByName('Lanc').AsString:= ELancamento.Text;
  end;

 if EDocumento.Text <> '' then
  begin
   QPesquisa.SQL.Add(' And (Lanc_Documento = :Doc)');
   QPesquisa.ParamByName('Doc').AsString:= EDocumento.Text;
  end;

 if DBLCBConta.Text <> '' then
  begin
   QPesquisa.SQL.Add(' And (Lanc_Conta = :Cont)');
   QPesquisa.ParamByName('Cont').AsInteger:= DBLCBConta.KeyValue;
  end;

 if ECheque.Text <> '' then
  begin
   QPesquisa.SQL.Add(' And (Lanc_Cheque = :Ch)');
   QPesquisa.ParamByName('Ch').AsString:= ECheque.Text;
  end;

 if DBLCBPlano.Text <> '' then
  begin
   QPesquisa.SQL.Add(' And (Lanc_Plano = :Plan)');
   QPesquisa.ParamByName('Plan').AsInteger:= DBLCBPlano.KeyValue;
  end;

  if (DEDataIni.Date <> 0) and (DEDataFim.Date <> 0) then
  begin
   QPesquisa.SQL.Add(' and (LANC_DATA  between :DataI and :DataF ) ');
   QPesquisa.ParamByName('DataI').AsDate:=DEDataIni.Date;
   QPesquisa.ParamByName('DataF').AsDate:=DEDataFim.Date;
  end;
 QPesquisa.Open;
 inherited;
 BtnImprimir.Enabled:=false;
end;

procedure TFPesquisaLancamento.CkBTodasEmpresasClick(Sender: TObject);
Var
 i :Integer;
begin
  inherited;
 for i := 0 to CLBEmpresas.Items.Count - 1 do
  begin
   if StrToInt(Copy(CLBEmpresas.Items.Strings[i],1,pos('-',CLBEmpresas.Items.Strings[i])- 1)) = Codigoempresa then
    CLBEmpresas.Checked[i]:=true
   else
    CLBEmpresas.Checked[i]:=CkBTodasEmpresas.Checked;
  end;
end;

procedure TFPesquisaLancamento.FormCreate(Sender: TObject);
begin
  inherited;
 QPesquisa.Close;
 QPesquisa.SQL.Clear;
 QPesquisa.SQL.Add('Select Emp_Codigo,Emp_Fantasia from empresa');
 QPesquisa.Open;
 QPesquisa.First;
 CLBEmpresas.Items.Clear;
 while not(QPesquisa.Eof) do
 begin
  CLBEmpresas.Items.Add(QPesquisa.fieldByname('Emp_Codigo').AsString + '-' + QPesquisa.fieldByname('Emp_Fantasia').AsString );
  QPesquisa.Next;
 end;
 CkBTodasEmpresasClick(Sender);
 QPesquisa.Close; 

end;

procedure TFPesquisaLancamento.BtnSelecionarClick(Sender: TObject);
begin
  inherited;
 if FLancamento <> nil then
  begin
   FLancamento.cdsCadastro.Close;
   FLancamento.cdsCadastro.Open;
   FLancamento.cdsCadastro.Locate('Lanc_Codigo',QPesquisa.fieldByname('Lanc_Codigo').AsString,[loCaseInsensitive]);
   FLancamento.PegaTitulo(false);
   FLancamento.Soma;
  end;
 Self.Close;
end;

procedure TFPesquisaLancamento.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
 if FLancamento <> nil then
  begin
   FLancamento.Enabled:=true;
   FLancamento.Show;
  end;
 FPesquisaLancamento:=nil;
 inherited;
end;

procedure TFPesquisaLancamento.BtnLimparClick(Sender: TObject);
begin
  inherited;
 ELancamento.Text:='';
 EDocumento.Text:='';
 DBLCBConta.KeyValue:=null;
 DBLCBPlano.KeyValue:=null;
 DEDataIni.Date:=0;
 DEDataFim.Date:=0;
end;

procedure TFPesquisaLancamento.DBLCBPlanoKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  inherited;
  case key of
  116 : begin //F5
          TDBLookupComboBox(Sender).ListSource.DataSet.Close;
          TDBLookupComboBox(Sender).ListSource.DataSet.Open;
        end ;
  46  : begin
          TDBLookupComboBox(Sender).KeyValue:=null;
        end;
 end;
end;

end.
